package org.example;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;

@RestController
public class StorageController {
    @GetMapping("product")
    String getData(){//超时控制
        try {
            Thread.sleep(10*1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(now);
        return "product storage: "+currentTime;
    }


    @GetMapping("productRandom")
    String getRandom(){//重试机制
        Random random = new Random();
        int a = random.nextInt(3)+1;
        try {
            if(a<=2){
                System.out.println("======请求随机接口======"+"模拟超时等待");
                Thread.sleep(10*1000);
            }else{
                System.out.println("======请求随机接口======"+"放行");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(now);
        return "product storage: "+currentTime;
    }

}
